/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

#include "sle_lite_tool.h"
#include <string.h>
extern void SleIperfToolAtCmdHandlerRegister(void);
static SleErrorCode InitSleTool(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = InitSleHostService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = InitSleAnnounceService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = InitSleSeekService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = InitSleConnectService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = InitSleSsapsService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = InitSleSsapcService();
    if(ret != SLE_NO_ERROR) return ret;
    return SLE_NO_ERROR;
}

static SleErrorCode DeinitSleTool(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = DeinitSleSsapcService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = DeinitSleSsapsService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = DeinitSleConnectService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = DeinitSleSeekService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = DeinitSleAnnounceService();
    if(ret != SLE_NO_ERROR) return ret;
    ret = DeinitSleHostService();
    if(ret != SLE_NO_ERROR) return ret;
    return SLE_NO_ERROR;
}

static SleErrorCode RegisterSleToolCbs(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = RegisterHostCallbacks(&g_hostToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = RegisterAnnounceCallbacks(&g_announceToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = RegisterSeekCallbacks(&g_seekToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    // ret = RegisterConnectCallbacks(&g_connectToolCbs);
    // if(ret != SLE_NO_ERROR) return ret;

    // ret = RegisterSsapServerCallbacks(&g_ssapsToolCbs);
    // if(ret != SLE_NO_ERROR) return ret;
    
    ret = RegisterSsapClientCallbacks(&g_ssapcToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = RegisterSsapServerCallbacks(&g_ssapsToolDelayLiteCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = RegisterConnectCallbacks(&g_connectToolDelayLiteCbs);
    if(ret != SLE_NO_ERROR) return ret;

    return SLE_NO_ERROR;
}

static SleErrorCode UnregisterSleToolCbs(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = UnregisterHostCallbacks(&g_hostToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = UnregisterAnnounceCallbacks(&g_announceToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = UnregisterSeekCallbacks(&g_seekToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = UnregisterConnectCallbacks(&g_connectToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = UnregisterSsapServerCallbacks(&g_ssapsToolCbs);
    if(ret != SLE_NO_ERROR) return ret;

    ret = UnregisterSsapClientCallbacks(&g_ssapcToolCbs);
    if(ret != SLE_NO_ERROR) return ret;
    return SLE_NO_ERROR;
}

void AppSleToolStart(void)
{
    printf("app_SleToolStart in\r\n");
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = InitSleTool();
    if(ret != SLE_NO_ERROR) goto fail;
    ret = RegisterSleToolCbs();
    if(ret != SLE_NO_ERROR) goto fail;
    RegisterToolFunction(SleHostToolCmdHandler, "--SleHostTool", "use :[AT+KHCOMMONTOOL=--SleHostTool 0] to show menu of sle_host_tool.");
    RegisterToolFunction(SleAnnounceToolCmdHandler, "--SleAnnounceTool", "use :[AT+KHCOMMONTOOL=--SleAnnounceTool 0] to show menu of sle_announce_tool.");
    RegisterToolFunction(SleSeekToolCmdHandler, "--SleSeekTool", "use :[AT+KHCOMMONTOOL=--SleSeekTool 0] to show menu of sle_seek_tool.");
    RegisterToolFunction(SleConnectToolCmdHandler, "--SleConnectTool", "use :[AT+KHCOMMONTOOL=--SleConnectTool 0] to show menu of sle_connect_tool.");
    RegisterToolFunction(SleSsapServerToolCmdHandler, "--SleSsapServerTool", "use :[AT+KHCOMMONTOOL=--SleSsapServerTool 0] to show menu of sle_ssap_server_tool.");
    RegisterToolFunction(SleSsapClientToolCmdHandler, "--SleSsapClientTool", "use :[AT+KHCOMMONTOOL=--SleSsapClientTool 0] to show menu of sle_ssap_client_tool.");    
    RegisterToolFunction(SleTestToolCmdHandler, "--SleTestTool", "use :[AT+KHCOMMONTOOL=--SleTestTool 0] to show menu of sle_test_tool.");
    SleIperfToolAtCmdHandlerRegister();
    return;
fail:
    ret = UnregisterSleToolCbs();
    ret = DeinitSleTool();
}

#ifdef __cplusplus
}
#endif // __cplusplus
